#include "guictrl.h"

#include "dev.h"
#include "msg.h"
#include "log.h"
#include "platform.h"

#include <stdlib.h>
#include <stdio.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pthread.h>
#include <unistd.h>


#define MSGMAXLEN 512

#define CAN_SERVICE "/usr/data/testlocket"

struct t_pipemsg
{
    unsigned int type;
    unsigned int gunno;
    unsigned int state;
    unsigned char detail[MSGMAXLEN];
};

struct t_localsocketket
{
    int socket_fd;   
	
};


struct guictrl_info{
	char select_id;     //用户选择要操作的id，默认为1
	struct t_localsocketket socket;
    os_mutex mutex;
};

static struct guictrl_info GI;


int socket_init(struct t_localsocketket *psocket)
{
    int ret;      
    static struct sockaddr_un srv_addr;  
 
    //创建socket 
    psocket->socket_fd = socket(PF_UNIX,SOCK_STREAM,0);  
    if(psocket->socket_fd<0)  
    {  
        perror("cannot create communication socket");  
        return -1;  
    }     
    srv_addr.sun_family=AF_UNIX;  
    strcpy(srv_addr.sun_path,CAN_SERVICE);  
 
    //连接到服务器  
    ret=connect(psocket->socket_fd,(struct sockaddr*)&srv_addr,sizeof(srv_addr));  
    if(ret==-1)  
    {  
        perror("cannot connect to the server");  
        close(psocket->socket_fd); 
        psocket->socket_fd = 0;
        return -1;  
    }  
	printf("init success\n");
	return 0;//成功了
}


int guictrl_send(int gunid, int type, int state, char *para, int len){
	struct t_pipemsg msg;
    if (len > sizeof(msg.detail)){
        log(LOG_ERR, "send len:%d err\r\n", len);
        return -1;
    }
    memset(&msg, 0, sizeof(msg));
	msg.type = type;
    msg.gunno = gunid;
    msg.state = state;
    //printf("gui send :type:%d gunid:%d state:%d\r\n", type, gunid, state);
    memcpy(msg.detail, para, len);
    os_mutex_lock(&GI.mutex);
    int ret = send(GI.socket.socket_fd, (unsigned char *)&msg, sizeof( struct t_pipemsg), 0);
    mSleep(300);
    os_mutex_unlock(&GI.mutex);
    return ret;

} 

static void *guictrl_recv(void *arg){
    struct t_pipemsg msg;
    while(1){
        memset(&msg, 0, sizeof(msg));
        int readnum = read(GI.socket.socket_fd,(unsigned char *)&msg,sizeof(msg));
        if (readnum != 0){
            printf("guictrl_recv type:%d gun:%d state:%d\r\n", msg.type, msg.gunno, msg.state);
            if (msg.type == E_PIPETYPE_SIGNALS)//刷卡充电被点击
            {
                struct t_detailsig *sigdetail = ( struct t_detailsig *)msg.detail;
                if (sigdetail->sigtype == E_SIG_STOP){
                    dev_stopcharge_user(msg.gunno - 1);
                }

                if(sigdetail->sigtype == E_SIG_SAVE){
                    data_update_config();
                    char str[10]={0};
                    data_getstr("alarmcode", str, 5);
                    printf("test alarmcode:%s\r\n", str);
                }
                
                if (sigdetail->sigtype ==  E_SIG_GUNSELECT){ 
                    if (data_getint("chargetest") != 0){ //test charger
                        printf("E_PIPETYPE_SIGNALS\r\n");
                        dev_startcharge(msg.gunno-1);
                    }else{
                        dev_swipcard_select(msg.gunno);
                    }
                }
                if (sigdetail->sigtype == E_SIG_TIMEOUT || sigdetail->sigtype == E_SIG_RETURN){
                    dev_gui_return(msg.gunno);

                }  
            }
        }
    }
}

void guictrl_show(int gunid, int cmd, char *msg, int len){
    switch(cmd){
    case GUI_SHOW_IDLE:
        guictrl_send(gunid, E_PIPETYPE_CHANGESTATE, E_STATE_IDLE, msg, len);
    break;
    case GUI_SHOW_WAIT:
        guictrl_send(gunid, E_PIPETYPE_CHANGESTATE, E_STATE_WAITFORGUN, msg, len);
    break;
    case GUI_SHOW_CHARGE:
        guictrl_send(gunid, E_PIPETYPE_CHANGESTATE, E_STATE_CHARGING, msg, len);
    break;
    case GUI_SHOW_SETTLE:
        guictrl_send(gunid, E_PIPETYPE_CHANGESTATE, E_STATE_SETTLE, msg, len);
    break;
    case GUI_SHOW_ALARM:
        guictrl_send(gunid, E_PIPETYPE_CHANGESTATE, E_STATE_ALARM, msg, len);
    break;
    case GUI_SHOW_MSG:
        guictrl_send(gunid, E_PIPETYPE_CHANGESTATE, E_STATE_POINTMSGWITHTIME, msg, len);
    break;
    case GUI_UPDATE_CHARGE:
        guictrl_send(gunid, E_PIPETYPE_UPDATEDATA, E_STATE_CHARGING, msg, len);
    break;
    case GUI_SHOW_START_FAIL:
        guictrl_send(gunid, E_PIPETYPE_CHANGESTATE, E_STATE_STARTFAILED, msg, len);
    break;
    case GUI_UPDATE_NET_STATE:
        guictrl_send(gunid, E_PIPETYPE_SIGNALS, E_STATE_NULL, msg, len);
    break;
    default:
    break;
    }
}

void guictrl_poll(void){
	char cardid[17]={0};
	
	//轮询读卡器
	if (dev_isswipcard(cardid)){
		
	}

    
}

int guictrl_init(void){

    //启动界面进程
    char cmd[100]={0};
    sprintf(cmd, "/usr/app/app_dcgui %s -qws&", CAN_SERVICE);
    system(cmd);
    mSleep(2000);
    memset(&GI, 0, sizeof(GI));
	GI.select_id = 1;
	while(socket_init(&GI.socket) != 0)
	{
	    mSleep(1000);
	    log(LOG_ERR, "socket_init failed\r\n");
	#if 0
	    if (socket_init(&GI.socket) != 0){
           log(LOG_ERR, "socket_init failed\r\n");
           return -1;
        }
    #endif
	}    

    os_pthread tidp1;  
    int ret = os_pthread_create(&tidp1, NULL, guictrl_recv,NULL);
    if (ret == -1)//创建一个定时任务
    {
        printf("create tidp1 failed!\r\n");
        return -1;
    } 

    os_mutex_init(&GI.mutex,NULL);
    
	return 0;
}



